package com.zy.baselib

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment

fun Activity.showKeyboard(view: View) {
    AppEx.showKeyboard(view)
}

fun Fragment.showKeyboard(view: View) {
    AppEx.showKeyboard(view)
}

fun Activity.hideKeyboard(view: View) {
    AppEx.hideKeyboard(view)
}

fun Fragment.hideKeyboard(view: View) {
    AppEx.hideKeyboard(view)
}

object AppEx {

    fun showKeyboard(view: View) {
        val imm = view.context
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        view.requestFocus()
        imm.showSoftInput(view, 0)
    }

    fun hideKeyboard(view: View) {
        val imm = view.context
            .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
}